<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item
            label="名称"
            prop="name"
            :rules="[
              { required: true, message: '请输入名称', trigger: 'change' }
            ]"
            style="width: 100%"
          >
            <el-input v-model="form['name']" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="通道类型"
            prop="type"
            :rules="[
              { required: true, message: '请输入通道类型', trigger: 'change' }
            ]"
            style="width: 100%"
          >
            <el-input v-model="form['type']" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="配置"
            prop="setting"
            :rules="[
              { required: true, message: '请输入配置', trigger: 'blur' }
            ]"
            style="width: 100%"
          >
            <el-input v-model="form['setting']" type="textarea" placeholder="请输入配置信息" style="width: 400px;" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="状态"
            prop="state"
            :rules="{
              required: true,
              message: '请选择状态',
              trigger: 'change'
            }"
            style="width: 100%"
          >
            <el-radio-group v-model="form.state" style="width: 200px">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button
        :loading="loading"
        type="primary"
        @click="save('form')"
      >确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateObject, saveObject } from '@/api/authsetting/index'
export default {
  name: 'MobileChannelEntity',
  dicts: ['user_status'],
  data() {
    return {
      title: '移动端配置管理',
      loading: false,
      dialogFormVisible: false,
      fileList: [],
      status: [{ id: 0, value: false, label: '禁用' },
        { id: 1, value: true, label: '启用' }],
      form: {
        id: null,
        name: '',
        type: '',
        setting: '',
        state: true
      }
    }
  },
  methods: {
    openEntity(data) {
      this.fileList = []
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.form = Object.assign({}, data)
        this.title = '修改信息'
        this.dialogFormVisible = true
      } else {
        this.title = '新增数据项信息'
        this.dialogFormVisible = true
      }
    },
    save(formName) {
      this.loading = true
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            updateObject(this.form)
              .then(res => {
                this.$message.success('更新成功！')
                this.close()
              })
              .catch(() => {
                this.loading = false
                this.$message.error('更新失败！')
              })
          } else {
            saveObject(this.form)
              .then(res => {
                this.$message.success('保存成功！')
                this.close()
                this.loading = false
              })
              .catch(() => {
                this.loading = false
                this.$message.error('保存失败！')
              })
          }
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    }
  }
}
</script>

<style scoped></style>
